<?php
/**
 * I know no such things as genius,it is nothing but labor and diligence.
 * @copyright (c) 2015-2016 LXSEA All rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author <657306123@qq.com> LXSEA
 */

namespace Admin\Controller;

use Common\Util\AttachmentUtil;
use Think\Controller;

/**
 * 七牛上传
 * @package Home\Controller
 */
class QiniuController extends Controller {

	/**
	 * 上传文件
	 */
	public function upload() {
		$driver = C( 'PICTURE_UPLOAD_DRIVER' );
		$config = array(
			'maxSize' => 10 * 1024 * 1024 , // 10M 上传的文件大小限制 (0-不做限制)
			'exts' => array(
				'jpg' , 'gif' , 'png' , 'jpeg' ,
				'wav' , 'wma' , 'amr' , 'aac' , 'mp3' ,
				'mp4' , 'avi' , 'mov' , 'swf' ,
				'xml' , 'txt' , 'xls' , 'doc' , 'pdf' ,
				'zip' , 'rar' , 'gz' ,
				'' , '' , '' , '' ) , // 允许上传的文件后缀
			'rootPath' => '/Data/upload/images/'
		);
		if( empty( $_FILES ) ) $this->outputError( '没有上传文件！' );

		// 实例化上传类
		$upload = new \Think\Upload( $config , C( 'PICTURE_UPLOAD_DRIVER' ) , C( "UPLOAD_{$driver}_CONFIG" ) );
		// 上传文件
		$info = $upload->uploadOne( $_FILES );
		// 上传错误提示错误信息
		if( !$info ) $this->outputError( $upload->getError() );

		$this->outputSuccess( $info['url'] );
	}

	/**
	 * 上传图片
	 */
	public function picture() {
		$driver = C( 'PICTURE_UPLOAD_DRIVER' );
		$config = array(
			'maxSize' => 2097152 , // 2M 上传的文件大小限制 (0-不做限制)
			'exts' => array( 'jpg' , 'gif' , 'png' , 'jpeg' ) , // 允许上传的文件后缀
			'rootPath' => './Uploads/Picture/'
		);
		if( empty( $_FILES ) ) $this->outputError( '没有上传文件！' );
		// 实例化上传类
		$upload = new \Think\Upload( $config , C( 'PICTURE_UPLOAD_DRIVER' ) , C( "UPLOAD_{$driver}_CONFIG" ) );
		// 上传文件
		$info = $upload->uploadOne( $_FILES['imgFile']);
		// 上传错误提示错误信息
		if( !$info ) $this->outputError( $upload->getError() );
		$this->outputSuccess( $info['url'] );
	}

    /**
     * 后台上传图片
     */
    public function adminPicture() {
        $driver = C( 'PICTURE_UPLOAD_DRIVER' );
        $config = array(
            'maxSize' => 2097152 , // 2M 上传的文件大小限制 (0-不做限制)
            'exts' => array( 'jpg' , 'gif' , 'png' , 'jpeg' ) , // 允许上传的文件后缀
            'rootPath' => './Uploads/Picture/'
        );
        if( empty( $_FILES ) ) $this->outputError( '没有上传文件！' );
        // 判断附件是否已存在
        $file_md5 = get_file_hash($_FILES['imgFile']['tmp_name'],'md5');
        if($file_exists = AttachmentUtil::getAttachmentInfoByMd5($file_md5)){
            echo json_encode(array( 'code' => 1 , 'url' => $file_exists['path'] ) );exit();
        }
        // 实例化上传类
        $upload = new \Think\Upload( $config , C( 'PICTURE_UPLOAD_DRIVER' ) , C( "UPLOAD_{$driver}_CONFIG" ) );
        // 上传文件
        $info = $upload->uploadOne( $_FILES['imgFile']);
        // 上传错误提示错误信息
        if( !$info ) {
            echo json_encode(array('code' => 0, 'url' => '', 'info' => $upload->getError()));exit();
        }
        $file_info = [
            'user_id' => 0,
            'name' => $_FILES['imgFile']['name'],
            'mime' => $info['type'],
            'path' => $info['url'],
            'url'=>$info['url'],
            'thumb' => '',
            'ext' => $info['ext'],
            'size' => $info['size'],
            'md5' => $info['md5'],
            'sha1' => $info['sha1'],
            'module' => 'shop',
            'driver'=>'qiniu',
            'create_time'=>time(),
            'update_time'=>time()
        ];
        if ($file_add = AttachmentUtil::createAttachment($file_info)) {
            echo json_encode(array( 'code' => 1 , 'url' => $info['url'] ) );exit();
        } else {
            echo json_encode(array( 'code' => 0 , 'url' => '' ) );exit();
        }
    }

	/**
	 * 输出到前端
	 * @param string $error
	 * @param string $url
	 * @param array $extend
	 */
	protected function output( $error , $url , $extend = array() ) {
		exit( json_encode( array_merge( array( 'error' => $error , 'url' => $url ) , $extend ) ) );
	}

	/**
	 * 错误输出到前端
	 * @param string|\Exception $error
	 * @param array $extend
	 */
	protected function outputError( $error = 'fail' , $extend = array() ) {
		if( $error instanceof \Exception ) {
			$error = $error->getMessage();
		}
		$this->output( $error , '' , $extend );
	}

	/**
	 * 成功输出到前端
	 * @param string $url
	 * @param array $extend
	 */
	protected function outputSuccess( $url , $extend = array() ) {
		$this->output( 0 , $url , $extend );
	}
}